package binary_tree.leetcode;

/**
 * 合并二叉树
 * @Author Stark
 * @Date 2022/3/3 15:56
 */
public class Num617_MergeTrees {
    public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
        //哪个结点为空就返回这个结点
        if(root1 == null){
            return root2;
        }
        if(root2 == null){
            return root1;
        }
        //走到这里说明两个结点都不为空,在root2上进行操作
        root2.val = root1.val + root2.val;
        //递归根结点的左右子树
        root2.left = mergeTrees(root1.left,root2.left);
        root2.right = mergeTrees(root1.right,root2.right);
        return root2;
    }
}